﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SS2TC.Main
{
    /// <summary>
    /// Writes data to CSV.
    /// </summary>
    internal class CsvWriter
    {
        private string directory;

        internal CsvWriter(string directory)
        {
            this.directory = directory;
        }

        /// <summary>
        /// Writes raw sample data to CSV from database.
        /// </summary>
        internal void WriteRawDataFromDatabase()
        {
            Queue<string> letters = new Queue<string>();
            Queue<byte[]> samples = new Queue<byte[]>();

            using (SqlConnection dbConnection = new SqlConnection(DatabaseStrings.SpeechDatabaseConnection))
            {
                dbConnection.Open();

                using (SqlCommand command = new SqlCommand(DatabaseStrings.SelectAllFromVowel, dbConnection))
                {
                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        string letter = reader["Letter"] as string;
                        byte[] data = reader["SampleData"] as byte[];
                        letters.Enqueue(letter);
                        samples.Enqueue(data);
                    }
                }

                dbConnection.Close();
            }

            int initialCount = letters.Count;
            int repeatedIndex = 1;

            for (int count = 0; count < initialCount; count++, repeatedIndex++)
            {
                string currentLetter = letters.Dequeue();
                byte[] currentSamples = samples.Dequeue();

                if (repeatedIndex == 11)
                {
                    repeatedIndex = 1;
                }

                StreamWriter csv = new StreamWriter(directory + currentLetter + repeatedIndex + ".csv");

                csv.WriteLine("SampleId, SampleData");

                for (int j = 0; j < currentSamples.Length; j++)
                {
                    csv.WriteLine((j + 1) + ", " + currentSamples[j]);
                }

                csv.Close();
            }
        }

        /// <summary>
        /// Writes sample frequeyncy data to CSV.
        /// </summary>
        internal void WriteFrequencyDataFromDatabase()
        {
            Queue<string> letters = new Queue<string>();
            Queue<byte[]> samples = new Queue<byte[]>();

            using (SqlConnection dbConnection = new SqlConnection(DatabaseStrings.SpeechDatabaseConnection))
            {
                dbConnection.Open();

                using (SqlCommand command = new SqlCommand(DatabaseStrings.SelectAllFromVowel, dbConnection))
                {
                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        string letter = reader["Letter"] as string;
                        byte[] data = reader["SampleData"] as byte[];
                        letters.Enqueue(letter);
                        samples.Enqueue(data);
                    }
                }

                dbConnection.Close();
            }

            int initialCount = letters.Count;
            int repeatedIndex = 1;

            for (int count = 0; count < initialCount; count++, repeatedIndex++)
            {
                string currentLetter = letters.Dequeue();
                byte[] currentSamples = samples.Dequeue();

                if (repeatedIndex == 11)
                {
                    repeatedIndex = 1;
                }

                StreamWriter csv = new StreamWriter(directory + currentLetter + repeatedIndex + "-freq.csv");

                csv.WriteLine("SampleData, Frequency");

                int freq = 0;
                for (int j = 0; j < currentSamples.Length; j++)
                {
                    if (j > 0 && (currentSamples[j] != currentSamples[j - 1]))
                    {
                        csv.WriteLine(currentSamples[j - 1] + ", " + freq);
                        freq = 1;
                    }
                    else
                    {
                        freq++;
                    }
                }

                // Last entry
                csv.WriteLine(currentSamples[currentSamples.Length - 1] + ", " + freq);

                csv.Close();
            }
        }
    }
}
